package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.reggie.domain.AddressBook;
import com.itheima.reggie.service.AddressBookServicel;
import com.itheima.reggie.utils.UserThreadLocal;
import com.itheima.reggie.vo.BaseContext;
import com.itheima.reggie.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("/addressBook")
@RestController
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookServicel addressBookServicel;

    /**
     * 地址查询
     *
     * @return
     */
    @GetMapping("/list")
    public R findAll(AddressBook addressBook) {
        //todo 从线程中获取id并且存入实体类
        addressBook.setUserId(UserThreadLocal.get().getId());
        log.info("addressBook:{}", addressBook);

        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        return R.success(addressBookServicel.list(queryWrapper));

    }

    /**
     * 添加地址
     *
     * @return
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {
        /*if (ObjectUtil.isAllNotEmpty(addressBook.getConsignee(), addressBook.getSex(), addressBook.getPhone()))
            return R.error("参数不合法");*/
        addressBook.setUserId(UserThreadLocal.get().getId());
        log.info("addressBook:{}", addressBook);
        addressBookServicel.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/default")
    public R defaultAddress(@RequestBody Map<String, String> param) {
        return addressBookServicel.defaultAddress(param);
    }
    /**
     * 查询默认地址
     *
     * @return
     */
    @GetMapping("/default")
    public R getDefaultAddressBook() {
        return R.success(addressBookServicel.getOne(Wrappers.lambdaQuery(AddressBook.class)
                .eq(AddressBook::getUserId,UserThreadLocal.get().getId())
                .eq(AddressBook::getIsDefault, 1)));
    }

    /**
     * 修改地址(回显)
     */

    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        AddressBook addressBook = addressBookServicel.getById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("没有找到该对象");
        }
    }

    /**
     * 修改地址（更新）
     */
    @PutMapping
    public R updateAddress(@RequestBody AddressBook addressBook) {
        return R.success(addressBookServicel.updateAddress(addressBook));
    }

    /**
     * 删除地址
     */
    @DeleteMapping
    public R deleteAddress(Long ids) {
        return R.success(addressBookServicel.removeById(ids));
    }
}
